package com.practice.tetris.config;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: Zhouyue
 * Date: 2015/6/11 21:20
 */
public class GameConfig {
    /**
     * 程序窗口宽度
     */
    private int width;
    /**
     * 程序窗口高度
     */
    private int height;
    /**
     * 边框素材拐角像素数
     */
    private int corner;
    /**
     * 内边距
     */
    private int padding;
    /**
     * 图层配置
     */
    private List<LayerConfig> layerConfigs;

    public GameConfig() throws Exception{
        //配置文件位置
        URL cfgUrl = ConfigDemo.class.getResource("/cfg.xml");
        //使用dom4j读取文件
        SAXReader reader = new SAXReader();
        //文档对象
        Document doc = reader.read(cfgUrl);
        //获取根节点
        Element game = doc.getRootElement();
        Element frame = game.element("frame");
        this.width = Integer.parseInt(frame.attributeValue("width"));
        this.height = Integer.parseInt(frame.attributeValue("height"));
        this.corner = Integer.parseInt(frame.attributeValue("corner"));
        this.padding = Integer.parseInt(frame.attributeValue("padding"));
        List<Element> layers = frame.elements("layer");
        this.layerConfigs = new ArrayList<LayerConfig>();
        for (Element element : layers) {
            LayerConfig layerConfig = new LayerConfig();
            layerConfig.setClassName(element.attributeValue("className"));
            layerConfig.setX(Integer.parseInt(element.attributeValue("x")));
            layerConfig.setY(Integer.parseInt(element.attributeValue("y")));
            layerConfig.setWidth(Integer.parseInt(element.attributeValue("width")));
            layerConfig.setHeight(Integer.parseInt(element.attributeValue("height")));
            layerConfigs.add(layerConfig);
        }
    }
}
